/**
 * 修改手机号
 */
var getyzm_able = true;
var localStorageManagers = require("script/localStorageManagers.js");
var downloadUtils = require('scripts/Core/DownLoadUtil.js');
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
//plus初始化
mui.plusReady(plusReady);
//plusReady
function plusReady() {
	//手机号值
	loadMobile();
	//获取验证码
	mui('body').on('tap', '#codebtn1', function() {
		if (getyzm_able == true) {
			this.className = 'yb';
			timedCount1();
			sendCheckcode('');
		}
	});
	mui('body').on('tap', '#codebtn2', function() {
		if (getyzm_able == true) {
			var mobile = document.getElementById('newmobile').value;
			//手机格式
			if (!isFormat(mobile))
				return;
			//该手机号是否已绑定
			var url = serverAjax + 'mobile/user/isBindMobile.do';
			mui.ajax(url, {
				data: {
					mobile: mobile
				},
				type: 'post',
				dataType: 'json',
				timeout: 10000,
				success: function(data) {
					
					if (data != null && data.code == 0) {
						this.className = 'yb';
						timedCount();
						sendCheckcode(mobile);
					} else {
						mui.toast('当前手机号已经被绑定，请使用其他手机号');
					}

				},
				error: function(e1, e2, e3) {
					ajaxErrorTip();
					console.log(e1);
				}
			});

		}
		/*if (getyzm_able == true) {
			var mobile = document.getElementById('newmobile').value;
			this.className = 'yb';
			timedCount2();
			sendCheckcode(mobile);
		}*/
	});
	//下一步
	//mui('body').on('tap', '.nextsubmit', nextStep);
	//确定
	mui('body').on('tap', '.submit', saveMobile);
};
//发送验证码
function sendCheckcode(mobile) {
	//if (!isFormat(mobile))
	//			return;
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/user/getMobileTelCheckCode1.do';
	mui.ajax(url, {
		data: {
			userid: userid,
			mobile: mobile
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
		  if(data.responseCode==0){
		     mui.toast('发送成功');
		  }else{
		  	 mui.toast(data.responseInfo);
		  }
			//if(data==null||data.length==0)return;
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
/*function isFormat(mobile){
	var tips='';
if (mobile.length > 0) {
		var re = /(^1[3|5|8][0-9]{9}$)/;
		var tips = '';
		if (trim(mobile) != '') {
			if (!re.test(mobile)) {
				tips='请输入正确的手机号码';
			}
		}
	} else {
		tips="请输入手机号";
	}	
	return tips;
}*/
function isFormat(mobile) {
	var isCheck = true;
	if (mobile.length > 0) {
		var re = /(^1[3|5|8][0-9]{9}$)/;
		var tips = '';
		if (trim(mobile) != '') {
			if (!re.test(mobile)) {
				mui.toast('请输入正确的手机号码');
				isCheck = false;
			}
		}
	} else {
		mui.toast("请输入手机号");
		isCheck = false;
	}
	return isCheck;
}
//计时
/*function timedCount1() {
	var interval = 1000;
	var len = 20000;
	if (codebtn1.value == null || codebtn1.value < 10 || codebtn1.value == len) {
		codebtn1.value = len;
		getyzm_able = false;
		codebtn1.innerHTML = (len / 1000) + "秒后重发";
	}
	var i = codebtn1.value - interval;
	if (i <= 100) {
		getyzm_able = true;
		codebtn1.value = len;
		codebtn1.innerHTML = "获取验证码";
		codebtn1.className = 'yb able';
		return;
	}
	codebtn1.value = i;
	codebtn1.innerHTML = (i / 1000) + "秒后重发";
	setTimeout("timedCount1()", interval);
} //计时
function timedCount2() {
	var interval = 1000;
	var len = 20000;
	if (codebtn2.value == null || codebtn2.value < 10 || codebtn2.value == len) {
		codebtn2.value = len;
		getyzm_able = false;
		codebtn2.innerHTML = (len / 1000) + "秒后重发";
	}
	var i = codebtn2.value - interval;
	if (i <= 100) {
		getyzm_able = true;
		codebtn2.value = len;
		codebtn2.innerHTML = "获取验证码";
		codebtn2.className = 'yb able';
		return;
	}
	codebtn2.value = i;
	codebtn2.innerHTML = (i / 1000) + "秒后重发";
	setTimeout("timedCount2()", interval);
}*/
//加载手机号
function loadMobile() {
	if (hasNet()) {
		loadAjax();
	} else {
		loadLocal();
	}
}
//是否有网
function hasNet() {
	var isNet = mobileFrame.IsNetWorkCanUse();
	return isNet;
}
//ajax加载
function loadAjax() {
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/user/getUser.do';
	mui.ajax(url, {
		data: {
			userid: userid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data == null || data.length == 0) return;
			var userinfo = data.ext1;
			localStorageManagers.setUserinfoSession(userinfo);
			userinfo = JSON.parse(userinfo);
			//document.getElementById('oldmobile').value = userinfo.telephone;
		},
		error: function(e1, e2, e3) {
			console.log(e1);
		}
	});
}
//本地加载
function loadLocal() {
	var userinfo = localStorageManagers.getUserinfoSession();
	document.getElementById('oldmobile').innerHTML = userinfo.telephone;
}
//下一步
/*function nextStep() {
	var code = document.getElementById('code1').value;
	code = trim(code);
	if (code == '') {
		mui.toast('请输入验证码');
		return;
	}
	var url = serverAjax + 'mobile/user/checkCheckCode.do';
	mui.ajax(url, {
		data: {
			code: code
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data.code == 0) {
				getyzm_able = true;
				var code1 = document.getElementById('code1').value;
				document.getElementById('m1').className = 'mdiv';
				document.getElementById('m2').className = 'mdiv act';
			} else {
				mui.toast('验证码错误');
			}
		},
		error: function(e1, e2, e3) {
			console.log(e1);
		}
	});
}*/
//保存手机号
function saveMobile() {
	if(!mobileFrame.IsNetWorkCanUse())return;
	var mobile = document.getElementById('newmobile').value;
	if (!isFormat(mobile))
				return;
	var code = document.getElementById('code2').value;
	code = trim(code);
	if (code == '') {
		mui.toast('请输入验证码');
		return;
	}
	
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + 'mobile/user/updateMobile.do';
	mui.ajax(url, {
		data: {
			mobile:mobile,
			userid: userid,
			code: code
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data.code == 0) {
				var parentWebView = plus.webview.getWebviewById("setup/accountindex");
	            parentWebView.evalJS("setMobile('" + mobile + "')");
				mui.toast('修改手机号成功');
				mui.back();
			} else if(data.code == 4){
				mui.toast('当前手机号与接收验证码手机号不一致,请修改');
			}else if(data.code == 2){
				mui.toast('验证码已过期');
			}else {
				mui.toast('验证码错误');
			}
		},
		error: function(e1, e2, e3) {
			console.log(e1);
		}
	});
}


function trim(str) {
	var strnew = str.replace(/^\s*|\s*$/g, "");
	return strnew;
}

//计时
function timedCount() {
	var interval = 1000;
	var len = 60000;
	if (codebtn2.value == null || codebtn2.value < 10 || codebtn2.value == len) {
		codebtn2.value = len;
		getyzm_able = false;
		codebtn2.innerHTML = (len / 1000) + "秒后重发";
	}
	var i = codebtn2.value - interval;
	if (i <= 100) {
		getyzm_able = true;
		codebtn2.value = len;
		codebtn2.innerHTML = "获取验证码";
		codebtn2.className = 'yb able';
		return;
	}
	codebtn2.value = i;
	codebtn2.innerHTML = (i / 1000) + "秒后重发";
	setTimeout("timedCount()", interval);
}